﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace UIT_CV_Management
{
    public class ChiTietNoiHoc
    {
        public String maChiTietNoiHoc;
        public String maCheDoHoc;
        public String maTieuLuan;
        public String maTotNghiep;


        private String tenTruong;
        private String tenThanhPho;
        private String nganhHoc;
        private DateTime thoiGianBatDau;
        private DateTime thoiGianKetThuc;


        //object
        public TieuLuan tieuLuan;
        public TotNghiep totNghiep;
        public CheDoHoc cheDoHoc;
        

        //database
        Database myDatabase = Database.shareDatabase;

        public CheDoHoc getCheDoHoc()
        {
            return CheDoHoc.cheDoHocFormID(maCheDoHoc);
        }
        public TotNghiep getTotNghiep()
        {
            return new TotNghiep(maTotNghiep);
        }
        public TieuLuan getTieuLuan()
        {
            return new TieuLuan(maTieuLuan);
        }
        public ChiTietNoiHoc(String _tenTruong, String _tenThanhPho, String _nganhHoc, DateTime _thoigianBatDau
            , DateTime _thoiGianKetThuc, TieuLuan _tieuLuan, TotNghiep _totNghiep, CheDoHoc _cheDoHoc)
        {
            maChiTietNoiHoc = Database.getID("ChiTietNoiHoc", "MaChiTietNoiHoc");

            //properties
            tenTruong = _tenTruong;
            tenThanhPho = _tenThanhPho;
            nganhHoc = _nganhHoc;
            thoiGianBatDau = _thoigianBatDau;
            thoiGianKetThuc = _thoiGianKetThuc;


            
            // ID
            maCheDoHoc = _cheDoHoc.maCheDoHoc;
            if (_tieuLuan != null)
                maTieuLuan = _tieuLuan.maTieuLuan;
            else
                maTieuLuan = "";
            if (_totNghiep != null)

                maTotNghiep = _totNghiep.maTotNghiep;
            else
                maTotNghiep = "";

            // object
            tieuLuan = _tieuLuan;
            totNghiep = _totNghiep;
            cheDoHoc = _cheDoHoc;
        }
        public ChiTietNoiHoc(String _maChiTietNoiHoc)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM ChiTietNoiHoc WHERE MaChiTietNoiHoc = '" + _maChiTietNoiHoc + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                maChiTietNoiHoc = table.Rows[0]["MaChiTietNoiHoc"].ToString();
                tenTruong = table.Rows[0]["TenTruong"].ToString();
                tenThanhPho = table.Rows[0]["TenThanhPho"].ToString();
                nganhHoc = table.Rows[0]["NganhHoc"].ToString();
                maCheDoHoc = table.Rows[0]["MaCheDoHoc"].ToString();
                thoiGianBatDau = table.Rows[0].Field<DateTime>("ThoiGianBatDau");
                thoiGianKetThuc = table.Rows[0].Field<DateTime>("ThoiGianKetThuc");
                maTieuLuan = table.Rows[0]["MaTieuLuan"].ToString();
                maTotNghiep = table.Rows[0]["MaTotNghiep"].ToString();
            }
        }

        public Boolean Save()
        {
            if (tieuLuan != null)
                tieuLuan.Save();

            if (totNghiep != null)
                totNghiep.Save();

            if (tieuLuan != null)
            {
                String sqlChiTietNoiHoc = "INSERT INTO ChiTietNoiHoc VALUES('"
                    + maChiTietNoiHoc + "',N'"
                    + tenTruong + "',N'"
                    + tenThanhPho + "',N'"
                    + nganhHoc + "',N'"
                    + maCheDoHoc + "','"
                    + Database.sqlDateTime(thoiGianBatDau) + "','"
                    + Database.sqlDateTime(thoiGianKetThuc) + "','"
                    + maTieuLuan + "',"
                    + "NULL)";
                QueryResult result = myDatabase.Query(sqlChiTietNoiHoc);
                if (result.ReturnCode == 409)
                    return false;
                return true;
            }
            if (totNghiep != null)
            {
                String sqlChiTietNoiHoc = "INSERT INTO ChiTietNoiHoc VALUES('"
                    + maChiTietNoiHoc + "',N'"
                    + tenTruong + "',N'"
                    + tenThanhPho + "',N'"
                    + nganhHoc + "',N'"
                    + maCheDoHoc + "','"
                    + Database.sqlDateTime(thoiGianBatDau) + "','"
                    + Database.sqlDateTime(thoiGianKetThuc) + "',"
                    +  "NULL,'"
                    + maTotNghiep +"')";
                QueryResult result = myDatabase.Query(sqlChiTietNoiHoc);
                if (result.ReturnCode == 409)
                    return false;
                return true;
            }
            return true;
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("ChiTietNoiHoc", "MaChiTietNoiHoc", maChiTietNoiHoc);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE ChiTietNoiHoc SET "
                    + "TenTruong = N'" + tenTruong
                    + "', TenThanhPho = N'" + tenThanhPho 
                    + "',MaCheDoHoc = '" + maCheDoHoc
                    + "', NganhHoc = N'" + nganhHoc 
                    + "',ThoiGianBatDau = '" + Database.sqlDateTime(thoiGianBatDau)
                    + "', ThoiGianKetThuc = '" + Database.sqlDateTime(thoiGianKetThuc)
                    + "' WHERE MaChiTietNoiHoc = '" + maChiTietNoiHoc + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;
                else
                {
                    return true;
                }

            }
            return false;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM ChiTietNoiHoc WHERE MaChiTietNoiHoc = '" + maChiTietNoiHoc + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            else
            {
                tieuLuan.Delete();
                totNghiep.Delete();
                return true;
            }
        }
    }
}
